package com.slipper.system.codeless.service;

import java.util.Collections;
import java.util.List;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.slipper.system.codeless.bo.DataFormDisplayBO;
import com.slipper.util.Tool;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;

import com.slipper.system.codeless.dao.DataFormDisplayDao;

import com.slipper.core.transaction.ReadTransactional;
import com.slipper.core.transaction.WriteTransactional;
import com.slipper.util.UUID;

@Service
@ReadTransactional
public class DataFormDisplayService {
    @Resource
    private DataFormDisplayDao dataFormDisplayDao;
    @Resource
    private DataFormDisplayRuleService dataFormDisplayRuleService;
    
    public DataFormDisplayBO queryBy(String id) {
        return dataFormDisplayDao.queryBy(id);
    }
    
    public List<DataFormDisplayBO> queryList(DataFormDisplayBO bo) {
        return dataFormDisplayDao.queryList(bo);
    }
    
    public PageInfo<DataFormDisplayBO> queryPage(DataFormDisplayBO bo) {
        PageHelper.startPage(bo.getPageNumber(), bo.getPageSize());
        return new PageInfo<>(dataFormDisplayDao.queryList(bo));
    }
    
    @WriteTransactional
    public int save(DataFormDisplayBO bo) {
        if (Tool.isNotBlank(bo.getId())) {
            bo.setUpdateTime(System.currentTimeMillis());
            return dataFormDisplayDao.update(bo);
        } else {
            bo.setId(UUID.getUUID());
            bo.setUpdateTime(System.currentTimeMillis());
            bo.setCreateTime(System.currentTimeMillis());
            return dataFormDisplayDao.insert(bo);
        }
    }

    @WriteTransactional
    public int save(String formId, List<DataFormDisplayBO> list, String employeeId) {
        if (Tool.isBlank(formId)) {
            return 0;
        }

        list.forEach(display -> {
            display.setFormId(formId);
            if (Tool.isBlank(display.getId())) {
                display.setId(UUID.getUUID());
                display.setUpdateId(employeeId);
                display.setCreateId(employeeId);
                display.setUpdateTime(System.currentTimeMillis());
                display.setCreateTime(System.currentTimeMillis());
                dataFormDisplayDao.insert(display);
            } else {
                display.setUpdateId(employeeId);
                display.setUpdateTime(System.currentTimeMillis());
                dataFormDisplayDao.update(display);
            }

            dataFormDisplayRuleService.save(display.getId(), display.getRuleList(), employeeId);
        });

        // 删除被删除的数据
        DataFormDisplayBO qo = new DataFormDisplayBO();
        qo.setFormId(formId);
        dataFormDisplayDao.queryList(qo).stream()
                .filter(display -> list.stream().noneMatch(temp -> display.getId().equals(temp.getId())))
                .forEach(this::delete);

        return 1;
    }

    @WriteTransactional
    public void saveList(List<DataFormDisplayBO> list) {
        if (list == null) return;

        list.forEach(item -> {
            if (dataFormDisplayDao.queryBy(item.getId()) != null) {
                item.setUpdateTime(System.currentTimeMillis());
                dataFormDisplayDao.update(item);
            } else {
                item.setUpdateTime(System.currentTimeMillis());
                item.setCreateTime(System.currentTimeMillis());
                dataFormDisplayDao.insert(item);
            }
        });
    }

    @WriteTransactional
    public void copyByFormId(String oldFormId, String newFormId, String employeeId) {
        if (Tool.isBlank(oldFormId) || Tool.isBlank(newFormId)) {
            return;
        }

        DataFormDisplayBO qo = new DataFormDisplayBO();
        qo.setFormId(oldFormId);
        dataFormDisplayDao.queryList(qo).forEach(display -> {
            String oldId = display.getId();
            display.setId(UUID.getUUID());
            display.setFormId(newFormId);
            display.setUpdateId(employeeId);
            display.setCreateId(employeeId);
            dataFormDisplayDao.insert(display);
            dataFormDisplayRuleService.copyByDisplayId(oldId, display.getId(), employeeId);
        });
    }

    public List<DataFormDisplayBO> queryByFormId(String formId) {
        if (Tool.isBlank(formId)) {
            return Collections.emptyList()  ;
        }

        DataFormDisplayBO qo = new DataFormDisplayBO();
        qo.setFormId(formId);
        List<DataFormDisplayBO> list = dataFormDisplayDao.queryList(qo);
        list.forEach(model -> model.setRuleList(dataFormDisplayRuleService.queryByDisplayId(model.getId())));
        return list;
    }

    @WriteTransactional
    public void deleteByFormId(String formId) {
        if (Tool.isBlank(formId)) {
            return;
        }

        DataFormDisplayBO qo = new DataFormDisplayBO();
        qo.setFormId(formId);
        dataFormDisplayDao.queryList(qo).forEach(this::delete);
    }
    
    @WriteTransactional
    public int delete(DataFormDisplayBO bo) {
        dataFormDisplayRuleService.deleteByDisplayId(bo.getId());
        return dataFormDisplayDao.delete(bo.getId());
    }
}
